//-----------------------------------------------------------------------------
// File: SinglePointShapeBase.cs
//
// Copyright (c) Dmitry Shuklin. All rights reserved.
//-----------------------------------------------------------------------------
using System;

namespace ObjectiveDraw.Platform
{
	/// <summary>
	/// Summary description for SinglePointShapeBase.
	/// </summary>
	public class SinglePointShapeBase: DrawerShapeBase
	{

		public SinglePointShapeBase()
		{
			this.m_MousePoints.Add(new DrawerShapes.MousePoint());
		}

		public float PointAbscissa
		{
			get
			{
				DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)this.m_MousePoints[0];
				return mp.Abscissa;
			}
			set
			{
				DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)this.m_MousePoints[0];
				mp.Abscissa = value;
			}
		}

		public float PointOrdinate
		{
			get
			{
				DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)this.m_MousePoints[0];
				return mp.Ordinate;
			}
			set
			{
				DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)this.m_MousePoints[0];
				mp.Ordinate = value;
			}
		}

		public Cerebrum.Direct3D.Forms.ShapeControl ControlPoint
		{
			get
			{
				return m_ControlPoints[this.m_MousePoints[0]] as Cerebrum.Direct3D.Forms.ShapeControl;
			}
		}


		public bool Creating = true;

		#region IDrawerShape Members

		public override bool UpdateShape(DrawerChartBase control, ChartInfoArgs cinfo, Cerebrum.Direct3D.Forms.DeviceEventArgs de, bool idle, out UpdateShapeInfo uinfo)
		{
			base.UpdateShape (control, cinfo, de, idle, out uinfo);

			if(m_Selected)
			{

				if(ControlPoint==null)
				{
					Cerebrum.Direct3D.Forms.ShapeControl frame = new Cerebrum.Direct3D.Forms.ShapeControl();
					frame.Z = 0.01f;
					frame.ForeColor = this.m_ShapeControlPrimaryColor;
					control.Controls.Insert(0, frame);
					this.AddControlPoint(this.m_MousePoints[0], frame);
				}
				


				System.Drawing.RectangleF _rect = control.SurfaceRectangle;

				float x1 = OffsetFromAbscissa(control, this.PointAbscissa);

				float y1 = OffsetFromOrdinate(control, this.PointOrdinate);

				float w = 10;
				float w2 = w / 2;

				ControlPoint.SetBounds(x1 - w2 - _rect.X, y1 - w2 - _rect.Y, w, w, System.Windows.Forms.BoundsSpecified.All);
			}
			return true;
		}

		public override void OnMouseDown(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e)
		{
			if(this.Creating)
			{
				this.PointAbscissa = AbscissaFromMouse(control, e.X);
				this.PointOrdinate = OrdinateFromMouse(control, e.Y);
			}
			else
			{
				m_DragState = new MouseDragState();
				foreach(DrawerShapes.MousePoint mp in this.m_MousePoints)
				{
					DrawerShapes.MousePoint mpc = mp.Clone() as DrawerShapes.MousePoint;
					mpc.Original = mp;
					m_DragState.MousePoints.Add(mpc);
				}
				m_DragState.MouseDownPoint = new System.Drawing.Point(e.X, e.Y);
			}
		}

		public override void OnMouseMove(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e)
		{
			DrawerChartContext context = control.Context;
			if(this.Creating)
			{
				this.PointAbscissa = AbscissaFromMouse(control, e.X);
				this.PointOrdinate = OrdinateFromMouse(control, e.Y);
			}
			else if(m_DragState!=null)
			{
				float _dx = (float)((m_DragState.MouseDownPoint.X - e.X) / context.AbscissaPosition.Scale);
				float _dy = (float)((m_DragState.MouseDownPoint.Y - e.Y) / context.OrdinatePosition.Scale);

				foreach(DrawerShapes.MousePoint mp in this.m_DragState.MousePoints)
				{
					mp.Original.Abscissa = mp.Abscissa - _dx;
					mp.Original.Ordinate = mp.Ordinate + _dy;
				}
			}
			control.Invalidate();
		}

		public override void OnMouseUp(ObjectiveDraw.Platform.DrawerChartBase control, System.Windows.Forms.MouseEventArgs e, ref bool CaptureMouse)
		{
			if(this.Creating)
			{
				this.PointAbscissa = AbscissaFromMouse(control, e.X);
				this.PointOrdinate = OrdinateFromMouse(control, e.Y);
				Creating = false;
			}
			m_DragState = null;
			control.Invalidate();
		}



		#endregion


		protected override bool LoadVariables(ObjectiveDraw.Platform.Serialization.FallbackChain storage, SerializationContext context)
		{
			bool r = base.LoadVariables(storage, context);
			/*--
			this.m_PointOrdinate = (float)Workspace.LoadVariable(this, KnownDrawerVariables.ShapeControlPointOrdinate + ".0", this.m_PointOrdinate);
			this.m_PointAbscissa = (System.DateTime)Workspace.LoadVariable(this, KnownDrawerVariables.ShapeControlPointDateTime + ".0", this.m_PointAbscissa);
			--*/
			this.Creating = false;
			/*for(int i = 0; i < 1; i++)
			{
				if(this.m_MousePoints[i]==null)
				{
					DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)Workspace.InitializeInstance(this.DomainContext, ObjectiveDraw.Platform.DrawerShapes.MousePoint.SystemComponentId, new SerializationContext(this, Cerebrum.SerializeDirection.Init, new System.Collections.Hashtable()));
					mp.Ordinal = i;
					this.m_MousePoints[i] = mp;
				}
			}*/
			return r;
		}

		protected override bool SaveVariables(ObjectiveDraw.Platform.Serialization.FallbackChain storage, SerializationContext context)
		{
			bool r = base.SaveVariables(storage, context);
			/*--
			Workspace.SaveVariable(this, KnownDrawerVariables.ShapeControlPointOrdinate + ".0", this.m_PointOrdinate);
			Workspace.SaveVariable(this, KnownDrawerVariables.ShapeControlPointDateTime + ".0", this.m_PointAbscissa);
			--*/
			return r;
		}

		/*protected override bool InitVariables(bool loading)
		{
			if(this.m_MousePoints==null)
			{
				this.m_MousePoints = new DrawerShapes.MousePoint[1];
			}
			if(!loading)
				for(int i = 0; i < 1; i++)
				{
					if(this.m_MousePoints[i]==null)
					{
						DrawerShapes.MousePoint mp = (DrawerShapes.MousePoint)Workspace.InitializeInstance(this.DomainContext, ObjectiveDraw.Platform.DrawerShapes.MousePoint.SystemComponentId, new SerializationContext(this, Cerebrum.SerializeDirection.Init, new System.Collections.Hashtable()));
						mp.Ordinal = i;
						this.m_MousePoints[i] = mp;
					}
				}
			return base.InitVariables (loading);
		}*/

		protected override void OnControlPointMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(m_MouseDown)
			{
				Cerebrum.Direct3D.Forms.Control ctl = sender as Cerebrum.Direct3D.Forms.Control;
				//ctl.SurfaceRectangle = new System.Drawing.RectangleF(new System.Drawing.PointF(m_DownMousePosition.X - (e.X + ctl.SurfaceRectangle.X), m_DownMousePosition.Y - (e.Y + ctl.SurfaceRectangle.Y)), ctl.Size);
				if(sender == this.ControlPoint)
				{
					this.PointAbscissa = AbscissaFromMouse((DrawerChartBase)ctl.Parent, (e.X + ctl.SurfaceRectangle.X) - m_DownMousePosition.X);
					this.PointOrdinate = OrdinateFromMouse((DrawerChartBase)ctl.Parent, (e.Y + ctl.SurfaceRectangle.Y) - m_DownMousePosition.Y);
					ctl.Invalidate();
				}
			}
			base.OnControlPointMouseMove(sender, e);
		}

	}
}
